package material.observer;

import java.util.Observable;
import java.util.Observer;

public class CurrentConditionsDisplayInner implements Observer, DisplayElement {
    Observable observable;
    private float temperature;
    private float humidity;

    public CurrentConditionsDisplayInner(Observable observable) {//构造函数的作用就在于将CurrentConditionsDisplayInner登记为观察者
        this.observable = observable;
        observable.addObserver(this);
    }


    @Override
    public void display() {
        System.out.println("当前温度：【" + this.temperature + "F degree】--当前湿度：【" + this.humidity + "% humidity】");
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherDataInner) {//首先确定观察者属于WeatherData类型的，然后利用setter方法获取温度和湿度测量值，最后调用display
            WeatherDataInner weatherData = (WeatherDataInner) o;
            this.temperature = weatherData.getTempature();
            this.humidity = weatherData.getHumidity();
            this.display();
        }

    }
}
